package file;

import java.io.*;
import java.util.Scanner;

//复制文件
public class Demo2 {
    public static void main(String[] args) throws IOException {
        System.out.println("请输入你要复制的文件的路径：");
        Scanner scan = new Scanner(System.in);
        String cloneFile = scan.next();
        File file = new File(cloneFile);
        if(!file.isFile()) {
            System.out.println("你选择的文件不存在或者路径不正确...");
            return;
        }
        System.out.println("请输入复制到哪里的路径：");
        String destPath = scan.next();
        File destFile = new File(destPath);
        if(!destFile.getParentFile().isDirectory()) {
            System.out.println("输入的路径不存在或者有误...");
            return;
        }

        //复制
        //字节流
        try(InputStream inputStream = new FileInputStream(file);
            OutputStream outputStream = new FileOutputStream(destFile)) {
            byte[] b = new byte[1024];
            while(true) {
                int n = inputStream.read(b);
                if(n == -1) {
                    break;
                }
                outputStream.write(b,0,n);
            }
            System.out.println("复制成功!!!");
        }
    }
}
